#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int inf=2e9;
const int Maxn=1e6+6,Maxm=4e6+6;
int n;
int a[Maxm],c,k[Maxn];
int b[Maxm];

vector<int>v[Maxm];
int id[Maxn],cnt;

inline void add(int x){
	for(auto i:v[x]){
		if(!id[i]) cnt++;
		id[i]++;
	}
}
inline void del(int x){
	for(auto i:v[x]){
		id[i]--;
		if(!id[i]) cnt--;
	}
}

inline int read() {
    char c = getchar();
    bool sign = (c == '-');
    
    while (c < '0' || c > '9')
        c = getchar(), sign |= (c == '-');
    
    int x = 0;
    
    while ('0' <= c && c <= '9')
        x = (x << 1) + (x << 3) + (c & 15), c = getchar();
    
    return sign ? (~x + 1) : x;
}

int main(){
	freopen("contest.in","r",stdin);
	freopen("contest.out","w",stdout); 

	n=read();
	for(int i=1;i<=n;i++){
		k[i]=read();
		for(int j=1;j<=k[i];j++){
			++c;
			b[c]=read(); 
			a[c]=b[c];
		}
	}
	sort(a+1,a+c+1);
	int m=unique(a+1,a+c+1)-a-1;
	c=0;
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=k[i];j++){
			++c;
			b[c]=lower_bound(a+1,a+m+1,b[c])-a;
			v[b[c]].emplace_back(i);
		}
	}
	
	int r=0; int ans=inf;
	for(int l=1;l<=m;l++){
		while(r<m and cnt!=n) add(++r);
		if(cnt==n) ans=min(ans,a[r]-a[l]);
		del(l);
	}	
	printf("%d",ans);

	return 0;
}
/*
4
9 -5 -9 2 8 5 4 3 3 8
2 10 8
1 -7
3 1 6 10
*/
